package org.dromara.tearoom.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.tearoom.service.IBreakerCommandService;
import org.dromara.tearoom.service.impl.GeeOpenCommandServiceImpl;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * 断路器回调接口
 * @author CKJ
 * @description: TODO
 * @date 2025/4/15 18:02
 */
@Slf4j
public class BreakerCallback implements IMqttMessageListener {

    private IBreakerCommandService iBreakerCommandService;

    public IBreakerCommandService getIBreakerCommandService() {
        if (iBreakerCommandService == null){
            iBreakerCommandService = SpringUtils.getBean(GeeOpenCommandServiceImpl.class);
        }
        return iBreakerCommandService;
    }

    /**
     * 订阅的回调
     * 消息到达的回调
     */
    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) {
        log.info("接收时间：{}", DateUtils.getTime());
        log.info("接收消息主题 :{}", topic);
        log.info("接收消息Qos : {}", mqttMessage.getQos());
        log.info("接收消息内容 : {}", new String(mqttMessage.getPayload()));
        log.info("接收消息retained :{}", mqttMessage.isRetained());
        try {
            getIBreakerCommandService().breakerCallback(topic, mqttMessage);
        } catch (Exception e) {
            log.warn("MQTT消息处理异常：{}", e.getMessage());
        }
    }

}
